<?php
/**
 * index.php     Zhuayi 新浪微博抽象类
 *
 * @copyright    (C) 2005 - 2010  Zhuayi
 * @licenes      http://www.zhuayi.net
 * @lastmodify   2010-10-27
 * @author       zhuayi
 * @QQ			 2179942
 */

class sina_action extends zhuayi
{

	/* 构造函数 */
	function __construct($oauth_token='')
	{
		parent::__construct();
		$this->app = $this->load_class('appsina',true);
		
		if (!empty($oauth_token))
		{
			$oauth_token = strings::json_dearray($oauth_token);
			
			$this->app->oauth_token = $oauth_token['oauth_token'];
			$this->app->oauth_token_secret = $oauth_token['oauth_token_secret'];
		}
	}
	

	/* 帐号登录 */
	function index()
	{
		$token = $this->app->request_token();
		
		$token = explode('&',$token);
		foreach ($token as $key=>$val)
		{
			$val = explode('=',$val);
			$list[$val[0]] = $val[1];
		}
		/* 记录获取来的token */
		cookie::set_cookie('sina_oauth_token',$list['oauth_token']);
		cookie::set_cookie('sina_oauth_token_secret',$list['oauth_token_secret']);
		$url = 'http://api.t.sina.com.cn/oauth/authorize?';
		$url .= 'oauth_token='.$list['oauth_token'];
		$url .= '&oauth_callback='.$this->weburl.'/sina/access/';
		$url .= '&display=page';
		
		header("Location:".$url);
		exit;
	}
	
	/* 得到了access 换取access_token*/
	function access()
	{
		$this->app->oauth_token = cookie::ret_cookie('sina_oauth_token');
		$this->app->oauth_token_secret = cookie::ret_cookie('sina_oauth_token_secret');
		$reset = $this->app->access_token($_GET['oauth_verifier']);
		
		if ($reset['error_code']!='-1')
		{
			echo $reset;
		}
		else
		{
			echo '-1';
		}
	}
	
	/* 帐号登录 */
	function login($data)
	{
		extract($data, EXTR_OVERWRITE);
		
		$this->app->agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Chrome/9.0.570.0 Safari/534.11';
		$this->app->cookies['user'] = cookie::ret_cookie('user');
		/*拉去token cookie */
		
		$this->app->fetchform($this->weburl.'/sina');
		
		/* 截取返回值里的 token 和 regCallback */
		$form['regCallback'] = strings::jiequ($this->app->results,'<input type="hidden" name="regCallback" value="','"');
		$form['oauth_token'] = strings::jiequ($this->app->results,'<input type="hidden" name="oauth_token" value="','"');
		$form['action'] = 'submit';
		$form['from'] = '';
		$form['oauth_callback'] = strings::jiequ($this->app->results,'<input type="hidden" name="oauth_callback" value="','"');
		$form['userId'] = $username;
		$form['passwd'] = $password;
		
		/* 判断下是否截取到了 */
		if (empty($form['oauth_token']))
		{
			return message::ret_json('-1','未截取到token,请检查网络...','array');
		}
		
		/* 模拟登录 */
		$this->app->submit('http://api.t.sina.com.cn/oauth/authorize',$form);
		
		if (strpos('^^'.$this->app->results,'oauth_token')==0)
		{
			return message::ret_json('-1','登录失败','array');
			
		}
		$token = explode('&',$this->app->results);
		foreach ($token as $key=>$val)
		{
			$val = explode('=',$val);
			$tokens[$val[0]] = $val[1];
		}
		/* 获取资料 */
		$this->app->oauth_token = $tokens['oauth_token'];
		$this->app->oauth_token_secret = $tokens['oauth_token_secret'];
		
		$reset = $this->app->verify_credentials();
		if ($reset['error_code'] == '-1')
		{
			return message::ret_json('-1','登录失败','array');
		}
		$return = $this->fields($reset);
		$return['token'] = strings::json_enarray($tokens);
		
		return $return;
	}
	
	/* 转换 */
	function fields($reset)
	{
		$return['aid'] = $reset['id'];
		$return['name'] = $reset['screen_name'];
		$return['address'] = $reset['location'];
		$return['description'] = $reset['description'];
		$return['face'] = $reset['profile_image_url'];
		$return['domain'] = 'http://t.sina.com/'.$reset['domain'];
		$gender = array('m'=>'1','f'=>'2');
		$return['sex'] = $gender[$reset['gender']];
		$return['followers_count'] = $reset['followers_count'];
		$return['friends_count'] = $reset['friends_count'];
		$return['statuses_count'] = $reset['statuses_count'];
		$return['dtime'] = strtotime($reset['created_at']);
		
		$return['verified'] = $reset['verified'];
		return $return;
	}
	/* 获取公共微博 */
	function public_timeline()
	{
		return  $this->app->public_timeline();
	}
	
	/* 获取微博列表 */
	function weibo_list($array)
	{
		
		return  $this->app->weibo_list($array);
	}
	
	/* 转发 */
	function weibo_repost($array)
	{
		extract($array, EXTR_OVERWRITE);
		
		return  $this->app->weibo_repost($id,$status,$is_comment);
	}
	

	/* 评论列表 */
	function comments_list($array)
	{
		
		return  $this->app->status_comments($array);
	}
	
	/* 评论 */
	function comment($array)
	{
		extract($array, EXTR_OVERWRITE);
		return  $this->app->comment($id,$comment,$cid);
	}
	
	/* 获取转发数和评论数 */
	function status_counts($ids)
	{
		return  $this->app->status_counts($ids);
	}
	
	/* 发布一条微博 */
	function weibo_info($status,$file='')
	{
		if (!empty($file))
		$file = str_replace($this->weburl,ZHUAYI_ROOT,$file);
		
		
		return $this->app->weibo_info($status,array('pic',$file));
	}
	
	/* 删除一条微博 */
	function delete($id)
	{
		return $this->app->statuses_destroy($id);
	}
	
	/* @atme 列表 */
	function atme($array)
	{
		return $this->app->atme($array);
	}
	
	/* 获取全部评论 */
	function comment_lists($act,$array)
	{
		
		if ($act == 'comments_to_me')
		{
			return $this->app->comments_to_me($array);
		}
		else
		{
			return $this->app->comments_by_me($array);
		}
	}
	
	/* 删除评论 */
	function comment_del($id)
	{
		return $this->app->comment_del($id);
	}
	
	/* 回复一条评论 */
	function comment_reply($array)
	{
		return $this->app->comment_reply($array);
	}
	
	/* 我的收藏 */
	function favorites()
	{
		$reset =  $this->app->favorites();
		return $reset;
	}
	
	/* 添加收藏 */
	function add_favorites($id)
	{
		$reset =  $this->app->add_favorites($id);
		return $reset;
	}
	
	/* 删除一个收藏 */
	function del_favorites($id)
	{
		$reset =  $this->app->del_favorites($id);
		return $reset;
	}
	
	/* 我的微博 */
	function my_weibo($array)
	{
		return $this->app->user_timeline($array);
	}
	
	/* 微博字段转换 */
	function array_key($array)
	{
		$array2['face'] = $array['head'].'/50';
		$array2['nick'] = $array['user']['name'];
		if (!empty($array['text']))
		{
			$array2['text'] = $array['text'];
		}
		else
		{
			$array['text'] = '转发：';
		}
		$array2['source'] = '来自'.$array['from'];
		
		if (!empty($array['image']))
		{
			$array2['litpic'] = $array['image'][0].'/160';
		}
		$array2['dtime'] = $this->load_fun('dtime',strtotime($array['created_at']),2);
		
		if (!empty($array['source']))
		{
			$array2['zhuanfa'] = $array['source'];
			$array2['zhuanfa']['nick'] = $array['source']['nick'];
			$array2['zhuanfa']['text'] = $array['source']['text'];
			
			if (!empty($array['zhuanfa']['image'][0]))
			{
				$array2['zhuanfa']['litpic'] = $array['zhuanfa']['image'][0].'/160';
			}
		}
		
		return $array2;
	}
	
	
	
	

}

?>